/**
 * Created by Arterli on 2015/4/26.
 */
module.exports = Controller("Admin/BaseController", function () {
    "use strict";
    return {
        indexAction: function () {
            this.assign({
                active: "category" //当前高亮
            });
            this.display();

        },
        categoryAction: function () {

            var self = this;

            if (this.isPost()) {
                D('category').select().then(function (data) {
                    self.json(get_children(data, 0));
                });
            }
        },
        addAction: function () { //编辑分类
            if (this.isPost()) {
                var param = this.post();
                console.log(param);
                var self = this;
                return D('category').add(param).then(function (id) {
                    return self.redirect('index');
                }).catch(function (err) {
                    return self.error(err);
                });
            } else {
                var cate = D('category').select().then(function (data) {
                    return get_children(data, 0);
                });

                this.assign({
                    category: cate,
                    active: "category" //当前高亮
                });

                this.display();
            }

        },
        editAction: function () { //编辑分类
            if (this.isPost()) {
                var param = this.post();
                console.log(param);
                var self = this;
                return D('category').where({cid:param.cid }).update(param).then(function (id) {
                    return self.redirect('index');
                }).catch(function (err) {
                    return self.error(err);
                });
            } else {
                var cid = this.get('cid');
                var catetable=D('category');
                var cate = catetable.select().then(function (data) {
                    return get_children(data, 0);
                });
                var data = catetable.where({cid:cid}).select().then(function (data) {
                    console.log(data)
                    return data;
                });
                var ischild=catetable.field('cid').where({pid:cid}).select().then(function(data){
                    if(data.length>0){
                        return true;
                    }else{
                        return false;
                    }

                })
                this.assign({
                    category: cate,
                    data:data,
                    ischild:ischild,
                    active: "category" //当前高亮
                });

                this.display();
            }

        },
        deleteAction: function () {//删除分类
            var self = this;
            if (this.isAjax('get')) {
                var id = this.get('cid');
                var cate=D('category');
                   cate.field('cid').where({pid:id}).select().then(function(data){
                        if(data.length>0){
                          self.json(0)
                        }else{
                            cate.where({cid:id}).delete().then(function (affectedRows) {
                                self.json(affectedRows)
                            })
                        }

                    })


            }
        }


    };
});